#ifndef DG5EMULATOR_H
#define DG5EMULATOR_H

#include <QtGui/QMainWindow>
#include "ui_dg5emulator.h"
#include "MiniComClient.h"

#include <qfile.h>
#include <qtextstream.h>
#include <QPointer>

namespace vkbrd
{
	class QtAsioThread;

	class Dg5Emulator : public QMainWindow
	{
		Q_OBJECT

	public:
		Dg5Emulator(QWidget *parent = 0, Qt::WFlags flags = 0);
		~Dg5Emulator();

		void closeEvent(QCloseEvent* _event);

signals:

		void dispatchWriteToPort(unsigned char msg);

		void stopIOThread();

		public slots:

			void sendTestMessage();

			void constructMessageFromSliders( char * msg);

			void constructMessageFromFile( char * msg );

			/**
			* Open file to read hex data.
			* @see sendTestMessage
			*/
			void openDatafile();

			/**
			* Check incoming bytes for special command bytes and
			* change sending state.
			*/
			void messageRecived(unsigned char* read_msg_, int bytes_transferred);

			void toggleSendSource(bool _value);

			/**
			* If _value is true, then create new ioThread and connect signals,
			* else cleanup thread.
			* Will send the same message with false param if port cannot be created.
			* @param
			*/
			void toggleSend(bool _value);

			/**
			* Tell ioThread to stop, wait for it to close, and delete it.
			*/
			void TerminateIOThread();

	private:

		QFile* inputFile;

		int pos;

		QTextStream* inputFileStream;

		Ui::Dg5EmulatorClass ui;

		//boost::thread* thread;
		QPointer<QtAsioThread>	ioThread;

		//!Send data in loop;
		bool sendingData;

		QList<QSlider*> sliders;

		void getSlidervalue(int _sliderNum, char* _msg,char _extraBits = 0);

		char getByteFromFile();

		//! Send data from file instead of sliders
		bool sendFromFile;

		bool closing;
	};

} //namespace vkbrd

#endif // DG5EMULATOR_H
